package servlet;

import dao.CartDAO;
import entity.Cart;
import entity.PageBean;
import org.bson.types.ObjectId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "cartListByUser",value = "/cart/userList")
public class CartListByUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        String id= request.getParameter("id");
        System.out.println(id);
        CartDAO cartDAO = new CartDAO();
        int pageNum = 1;
        int pageSize = 10;
        //2. 将pageNum pageSize === skip 和limit
        int skip = (pageNum-1)*pageSize;
        int limit = pageSize;
//        List<Cart> list = cartDAO.findPageByUserId(cart.getUserId(),skip,limit);
        List<Cart> list = cartDAO.findPageByUserId(new ObjectId(id),skip,limit);
        System.out.println(list);
        list.size();
        long total = cartDAO.getCountByUId(new ObjectId(id));
        PageBean<Cart> page = new PageBean<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setList(list);
        page.setTotal(total);
        page.setPageTotal(total,pageSize);
        request.setAttribute("page",page);
        request.getRequestDispatcher("/cart/userList.jsp").forward(request,resp);
        System.out.println("列表展示成功2");
    }
}
